package cn.z2huo.knowledge.collection.map.hashmap.compute;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * @author z2huo
 */
class Test {

    public static void main(String[] args) {
        Test test = new Test();
        test.absent();
        System.out.println();
        test.present();
    }

    public void present() {
        Map<String, String> map = new HashMap<>();
//        map.put("name", "zyz");
        System.out.println(map);
        String newValue = map.computeIfPresent("name", (key, value) -> key + value);
        System.out.println(newValue);
        System.out.println(map);
    }

    public void absent() {
        Map<String, String> map = new HashMap<>();
//        map.put("name", "zyz");
        System.out.println(map);
        String newValue = map.computeIfAbsent("name", key -> get(key));
        System.out.println(newValue);
        System.out.println(map);
    }

    public String get(String key) {
        return RandomStringUtils.randomAlphabetic(key.length());
    }

}
